package controller;

import exception.PlayerActionEx;
import exception.PlayerCountEx;
import poker.Table;
import poker.PlayerAction;

public class Poker {
	Table theTable;
	
	public Poker(Table table) {
		theTable = table;
	}
	
	public void auctionAction(String action, String amount) {
		try {
			if(action == "Check") {
				theTable.advance(PlayerAction.CHECK, 0);
			} else if(action == "Bet") {
				theTable.advance(PlayerAction.BET, 0);
			} else if(action == "Call") {
				theTable.advance(PlayerAction.CALL, 0);
			} else if(action == "Raise") {
				theTable.advance(PlayerAction.RAISE, Integer.parseInt(amount));
			} else if(action == "Fold") {
				theTable.advance(PlayerAction.FOLD, 0);
			} else if(action == "All-in") {
				theTable.advance(PlayerAction.ALLIN, 0);
			}
		} catch(IllegalArgumentException e) {
			try {
				theTable.advance(PlayerAction.RAISE, -1);
			} catch (PlayerActionEx paex) {
			} catch (Exception ex) {
				e.printStackTrace();
			}
			System.out.println("IllegalArgument: " + amount);
		} catch (PlayerActionEx paex) {
			System.out.println(paex.getMessage());
		} catch(Exception e) {
			e.printStackTrace();
		}
	}

	public void exchangeAction(boolean[] cardData) {
		int[] cards = new int[5];
		for(int i=0; i<5; i++) {
			if(cardData[i]) {
				cards[i] = i+1;
			}
		}
		try {
			theTable.advance(cards);
		} catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	public void disconnect(int playerNumber) {
		try {
			theTable.disconnectPlayer(playerNumber);
		} catch(Exception e) {
			e.printStackTrace();
		}
	}

	public void requestResults() {
		try {
			theTable.showResult();
		} catch(Exception e) {
			System.out.println(e.getMessage());
		}
	}
	
	public void replay() {
		try {
			theTable.prepareNextGame();
		} catch (PlayerCountEx pcex) {
			System.out.println("Brak graczy");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void playBotTurn() {
		try {
			theTable.handleBot();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
